package com.lyfei.ydc.domain.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.lyfei.ydc.domain.BaseEntity;
import com.lyfei.ydc.domain.dto.SkuInfoDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_order_sku")
public class OrderSku extends BaseEntity<OrderSku> {

    private Long orderId;
    private Long skuId;
    private Long spuId;
    private BigDecimal finalPrice;
    private BigDecimal singlePrice;
    private String specValues;
    private Integer count;
    private String img;
    private String title;

    public OrderSku(Sku sku, SkuInfoDto skuInfoDto) {

        this.skuId = sku.getId();
        this.spuId = sku.getSpuId();
        this.singlePrice = sku.getPrice();
        this.finalPrice = sku.getPrice().multiply(new BigDecimal(skuInfoDto.getCount()));
        this.count = skuInfoDto.getCount();
        this.img = sku.getImg();
        this.title = sku.getTitle();
        List<String> specValueList = sku.getSpecKeyValues().stream().map(SpecKeyValue::getValue).collect(Collectors.toList());
        if(!specValueList.isEmpty()) {
            this.specValues = String.join(",", specValueList);
        }
    }
}
